package com.gao.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Consumer;

/**
 * @Author: ShanYuqing
 * @Version: v1.0
 */
public class CollectionTest {
    /*
            public Iterator<E> iterator() : 获取集合的迭代器
     */
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        c.add("张三");
        c.add("李四");
        c.add("王五");
        //1.获取迭代器
        Iterator<String> iterator = c.iterator();
        //2.循环判断，集合中是否还有元素
        while (iterator.hasNext()) {
            //3.调用next方法,将元素取出
            String next = iterator.next();
            System.out.println(next);
        }
        System.out.println("============");

        //使用增强for循环遍历集合
        for(String s:c){
            System.out.println(s);
        }

        System.out.println("-----------------");

        //foreach 遍历集合
        c.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });


    }
}
